<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template #btns>
        <el-button v-if="meta.indexOf('CourseSaleDetailExport') > -1" :loading="exportLoading" type="primary" @click="batchexport">导出</el-button>
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">筛选</div>
        <div class="value">合计</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>¥</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
      <el-tooltip effect="dark" placement="left">
        <div class="jesm"><img style="width: 15px;marginRight: 2px;" src="@/assets/icon/question.png" alt="">金额说明</div>
        <div slot="content" v-html="smTip" />
      </el-tooltip>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        label="课程名称"
        min-width="140"
        prop="courseName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="课程类型"
        min-width="120"
        prop="courseType"
        show-overflow-tooltip
      >
        <template scope="scope">
          <span>{{ nameCourseType(scope.row.courseType) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="课程价格"
        prop="coursePrice"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="实付款"
        prop="realPrice"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="已退款"
        prop="refundPrice"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="订单号"
        min-width="180"
        prop="orderId"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="订单来源"
        prop="from"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.from == "01" ? "小程序" : "场馆PC端" }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="场馆名称"
        prop="venueName"
        min-width="130"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="支付方式"
        prop="payMethod"
        min-width="120"
        show-overflow-tooltip
      >
        <template scope="scope">
          <span>{{ nameWithPayMethod(scope.row.payMethod) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="用户信息"
        min-width="200"
        show-overflow-tooltip
        prop="memberInfo"
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作员"
        prop="creatUser"
        show-overflow-tooltip
        min-width="120"
      />
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { venueList, courseSaleDetailExcel, allConfigList, courseSaleDetail } from '@/api'
import { queryFormConfig } from './config'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      list: [],
      tjList: [],
      smTip: '',
      queryFormConfig,
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      exportLoading: false,
      payTypes: [],
      filters: {
        selTime: []
      },
      listLoading: true,
      show: false,
      orderId: '',
      form: {},
      rules: {
        price: [
          { pattern: /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/, required: true, message: '请输入正确课时总价(元),最多保留2位小数', trigger: 'blur' }
        ],
        info: [
          { required: true, message: '费用由线下退款，请具体描述退课原因', trigger: 'blue' }
        ]
      },
      meta: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    const end = new Date()
    const start = new Date()
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
    this.filters.selTime = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
    this.getVenueList()
    this.getDefalutConfig()
    this.getList(1)
  },
  methods: {
    nameWithPayMethod(payM) {
      const temp = this.payTypes.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    nameCourseType(type) {
      if (type === '0') {
        return '视频课'
      } else if (type === '1') {
        return '常规课'
      } else if (type === '4') {
        return '私教课'
      }
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    getVenueList() {
      venueList({
        param: { flag: '0' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          this.queryFormConfig.formItems[0].options = arr
          // this.$set(this.filters, 'venueId', res.recordList[0].id)
          this.getList()
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    getDefalutConfig() {
      allConfigList({ param: { type: '2', code: '1' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.payTypes = res.recordList
          const arr = res.recordList
          this.queryFormConfig.formItems[5].options = arr
        })
        .catch((err) => {
          console.log(err)
        })
    },
    clearList() {
      this.filters = {
        selTime: this.filters.selTime,
        venueId: this.filters.venueId
      }
      this.getList(1)
    },
    getList(page) {
      this.listLoading = true
      sessionStorage.setItem('venueId', this.filters.venueId || '')
      const { filters, pagination } = this
      const venueId = sessionStorage.getItem('venueId')
      filters.flag = '2'
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (filters.selTime && filters.selTime.length > 0) {
        filters.startTime = filters.selTime[0]
        filters.endTime = filters.selTime[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      pagination.firstQueryTime = parseTime(new Date())
      courseSaleDetail({ pagination, param: { venueId, ...filters }}).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          const tjList = res.data
          if (tjList) {
            let smTip = ''
            tjList.forEach(s => {
              smTip = smTip + s.tip + '<br />'
            })
            this.tjList = tjList
            this.smTip = smTip
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    batchexport() {
      sessionStorage.setItem('venueId', this.filters.venueId || '')
      const { filters } = this
      const venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      courseSaleDetailExcel({ param: { venueId, ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>
